package encaps.action;

import encaps.entities.Grant;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.utils.StringUtil;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import encaps.utils.search.SearchContext;
import encaps.utils.search.Searcher;
import org.jdom.Element;

public class AdvancedPersonSearchAction extends AbstractSearchAction {

    public static final String ADVANCED_PERSON_SEARCH = "advPersonSearch";
    public static final String NAME = "name";
    public static final String BIO = "bio";
    public static final String MOVIE_TITLE = "movie";

    public AdvancedPersonSearchAction(ActionContext context, Grant requiredGrant) {
        actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            Element response = XMLHelper.createElement(Response.RESPONSE);
            Element searchFormElement = createSearchFormElement();
            XMLHelper.addChild(response, searchFormElement);

            boolean doSearch = !isSearchingRequestEmpty();
            if (doSearch) {
                Searcher searcher = searchers.get(PERSON);
                SearchContext context = getSearchContext();
                Element foundResult = searcher.getResult(context);
                XMLHelper.addChild(response, foundResult);
            }

            return response;
        } catch (SystemException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        }
    }

    private Element createSearchFormElement() throws SystemException {
        Element form = XMLHelper.createElement(Response.ENTITY);
        form.setAttribute(Response.EDIT, "true");

        String name = StringUtil.getValidValue(actionContext.getTextParameter(NAME));
        String bio = StringUtil.getValidValue(actionContext.getTextParameter(BIO));
        String movieTitle = StringUtil.getValidValue(actionContext.getTextParameter(MOVIE_TITLE));

        XMLHelper.addChild(form, XMLHelper.createHeader(Labels.getLabel(Labels.PERSON_SEARCHING_HEADER)));
        XMLHelper.addChild(form, XMLHelper.createTextField(Labels.getLabel(Labels.PERSON_NAME), name, NAME));
        XMLHelper.addChild(form, XMLHelper.createTextField(Labels.getLabel(Labels.PERSON_BIOGRAPHY), bio, BIO));
        XMLHelper.addChild(form, XMLHelper.createTextField(Labels.getLabel(Labels.MOVIE_TITLE), movieTitle, MOVIE_TITLE));
        //XMLHelper.addChild(form, XMLHelper.createButton(Labels.getLabel(Labels.SEARCH_BUTTON), ADVANCED_PERSON_SEARCH));
        XMLHelper.addChild(form, XMLHelper.createImageButton(Image.getImage(Image.SEARCH), ADVANCED_PERSON_SEARCH));

        return form;
    }

    private boolean isSearchingRequestEmpty() throws SystemException {
        String showFormOnly = actionContext.getTextParameter(SHOW_SEARCH_FORM_ONLY);
        return StringUtil.isEmpty(showFormOnly) ? false : !showFormOnly.equalsIgnoreCase("false");
    }

    @Override
    protected SearchContext createEmptySearchContext() {
        return new SearchContext(false);
    }

    @Override
    protected void fillContextWithSearchingParameters(SearchContext context) throws SystemException {
        String name = StringUtil.getValidValue(actionContext.getTextParameter(NAME));
        context.addSearchParameter(NAME, name);
        String bio = StringUtil.getValidValue(actionContext.getTextParameter(BIO));
        context.addSearchParameter(BIO, bio);
        String movieTitle = StringUtil.getValidValue(actionContext.getTextParameter(MOVIE_TITLE));
        context.addSearchParameter(MOVIE_TITLE, movieTitle);

        context.setActionName(ADVANCED_PERSON_SEARCH);
    }
}
